param(
	[Parameter(Mandatory=$false)]                             
	[string]$inputFile="inputlist.txt",     # the input file to store vdi,domain and fid list
	[Parameter(Mandatory=$false)]                             
	[switch]$inputFileSaved,               # tell if inputfile has been saved, otherwise will popup text to let user enter VDI list.
	[Parameter(Mandatory=$false)]                             
	[switch]$functionInvoked              # tell if script needs to invoke functions
)
$st=get-date
$this_path = Split-Path -Parent ($MyInvocation.MyCommand.Definition) # get the directory of this running script
$parent_path=(get-item $this_path).Parent.FullName
If( !$functionInvoked ){
	. "$parent_path\functions.ps1" # import functions to powershell from functions.ps1
}
If( $inputFile -notlike "*\*"){
    $inputFile="$this_path\$inputFile"
}
$inputlist=Get-Inputlist $inputFile $inputFileSaved
#------------------------------------------------ Startup Folder Checking------------------------------------------------------------------
$startup_temp_path="$this_path\temp" # used to store each result csv file from end vdi incase the script breaks in the middle of running unexpectedly so that we can check the file and know which ones we have already checked
del "$startup_temp_path\*.csv" -Force -ErrorAction Ignore
del "$this_path\*.csv" -Force -ErrorAction Ignore
New-FileOrFolder $startup_temp_path -d
$startup_action={
	param($line,$cnt,$ActionParams)
	try{$vdi,$domain,$fid,$env=$line.split(",").trim()}catch{}
	write-host "Processing in $cnt-$vdi for Startup Folder checking...`n"
	$startup_temp_path=$ActionParams
	$startupfiles = [ordered]@{
		"Category/Setting"="$vdi/$fid/$domain";
		"StartupFolder/Contents"=$null;
		"StartupFolder/VNCShortcutName-LinkTo"=$null;
		"StartupFolder/VNCExePath-Version"=$null}
	If( [string]::IsNullOrWhiteSpace($fid) -or [string]::IsNullOrWhiteSpace($domain)){
		$startupfiles.Set_Item("Category/Setting","$vdi/FID/domain Not Provided")
		$rst= New-Object PSObject -Property $registries
		Write-Host -ForegroundColor Red "$cnt-$vdi FID Not Provided"
		return $rst
	}else{
		If( !(Check-FIDExistence $domain $fid) ){ # check if given FID exists in given domain 
			$startupfiles.Set_Item("Category/Setting","$vdi/FID $fid was not found in domain/$domain")
			$rst= New-Object PSObject -Property $registries
			Write-Host -ForegroundColor Red "$cnt-$vdi FID Not Provided"
			return $rst
		}
	}
	If(Test-Connection $vdi -quiet -count 1 -ErrorAction Ignore){ # write-host "Checking if $vdi is online"
		try{
			test-path "\\$vdi\c$\Users\$FID\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" -ErrorAction Stop 1>$null
		}catch{
			If($_.exception -like "*Access is denied*"){$startupfiles.Set_Item("Category/Setting","$vdi/$fid/$domain Access denied")}
			$rst= New-Object PSObject -Property $startupfiles
			Write-Host -ForegroundColor Red "$cnt-$vdi-Access denied"
			return $rst
		}
		Try{
			$value=$null; $vncPath=@() ; $vnc_exe=$null ; $vnc_shortcut=$null
			ls "\\$vdi\c$\Users\$FID\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"|%{$value+=$_.Name+"|"}
			$startupfiles.Set_Item("StartupFolder/Contents",$value)
			# check ultraVNC version
			$vnclink =ls "\\$vdi\c$\Users\$FID\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\*winvnc*.lnk" 2>$null
			If( $vnclink -ne $null ){
				$Shell = New-Object -ComObject WScript.Shell 
				For($i=0;$i -lt $vnclink.count;$i++){
					$vnc_shortcut_pro=$Shell.CreateShortcut($vnclink[$i]) ; $vnc_targetPath=$vnc_shortcut_pro.TargetPath
					$vnc_shortcut+=($vnclink[$i].Name+"|$vnc_targetPath")
					$vncPath+=("\\$vdi\"+($vnc_targetPath -replace(":","$")))
				}
				$startupfiles.Set_Item("StartupFolder/VNCShortcutName-LinkTo",$vnc_shortcut)
				$vncPath|%{ls $_|%{$vnc_exe+=($_.FullName+"|"+$_.VersionInfo.ProductVersion)}}
				$startupfiles.Set_Item("StartupFolder/VNCExePath-Version",$vnc_exe)
			}
		}Catch{
		    $startupfiles.Set_Item("StartupFolder/Contents","startup folder not exists")
		}Finally{}
	}else{
		$startupfiles.Set_Item("Category/Setting","$vdi-invalid vdi name or offline/$fid/$domain")
	}
	$rst= New-Object PSObject -Property $startupfiles
	Write-Host -ForegroundColor Yellow "----$cnt-$vdi Done checking Startup Folder info----`n"
	$rst|Export-Csv -Path "$startup_temp_path\$vdi-startup.csv" -UseCulture -NoTypeInformation -Encoding UTF8 -Force -ErrorAction Ignore
	return $rst
}
$startupfiles = Start-BulkThreads $inputlist $startup_action $startup_temp_path -maxCnt 100|sort "Category/Setting"
Export-Result $startupfiles "startup folder files" "$this_path\startup.csv" ([math]::Round(((get-date)-$st).totalseconds))
